export default {
    data(){
        return {
            localButtons:[]
        }
    },
    methods:{
        /**
         * 获取状态。
         * @param type
         * @param negation
         * @returns {boolean}
         */
        getStatus(type){
            let status="";
            let bpmInst=this.curVm.bpmInst;
            if(bpmInst && bpmInst.status){
                status=bpmInst.status;
            }
            let rtn= status== type;
            return rtn;
        },
        /**
         * 是否可以作废
         * @returns {boolean}
         */
        canCancel(){
            let status=this.curVm.bpmInst && this.curVm.bpmInst.status || "" ;
            let ary=["RUNNING","SUPSPEND"];
            if(ary.includes(status)){
                return true;
            }
            return false;
        },

        initButtons(){
            let self = this ;

            this.localButtons.length=0;

            let defaultBtn = [
                {
                    show:self.curVm.canTrack,
                    id:'btnTracked',
                    method:'doTracked',
                    style:false,
                    name:self.curVm.trackText,
                    defaultBtn:true
                },
                {
                    show:self.curVm.allowTransfer,
                    id:'btnTransfer',
                    method:'transferTo',
                    style:false,
                    name:self.curVm.i18n('list.forward','转发'),
                    defaultBtn:true,
                    "icon": "{type:'jpaasIcon',icon:'iconVue3jpaas-icon_huifuzhuanfa'}",
                },
                {
                    show:self.curVm.allowTransReply,
                    id:"btnTransReply",
                    method:'transferReply',
                    style:false,
                    name:self.curVm.i18n('list.replyFor','回复转发'),
                    defaultBtn:true,
                    "icon": "{type:'jpaasIcon',icon:'iconVue3jpaas-icon_huifuliuyan'}",
                },
                {
                    id:"btnBack",
                    method:'retrieveTaskNode',
                    style:false,
                    name:self.curVm.i18n('list.fetchTask','取回任务'),
                    show:self.curVm.canRevoke,
                    defaultBtn:true,
                    "icon": "{type:'jpaasIcon',icon:'iconVue3jpaas-icon_quhuirenwu'}",
                },
                {
                    show:self.curVm.bpmCommuBtnShow,
                    id:"btnCommuHistory",
                    method:'doCommuHistory',
                    style:false,
                    name:self.curVm.i18n('list.communicate','沟通情况'),
                    defaultBtn:true,
                    "icon": "{type:'jpaasIcon',icon:'iconVue3jpaas-icon_huifuliuyan'}",
                },
                {
                    show:self.curVm.showMsgBtn,
                    id:"btnMessage",
                    method:'showMsg',
                    style:false,
                    name:self.curVm.i18n('list.leaveMsg','留言'),
                    defaultBtn:true,
                    "icon": "{type:'jpaasIcon',icon:'iconmessage'}",
                },
                {
                    id:"btnPrint",
                    method:'formPrint',
                    style:false,
                    name:self.curVm.$ti18('rxForm.print','打印'),
                    defaultBtn:true,
                    "icon": "{type:'jpaasIcon',icon:'iconVue3jpaas-icon_print'}",
                },
                {
                    show:self.getStatus('SUPSPEND'),
                    id:"restoreInst",
                    method:'restoreInst',
                    style:false,
                    name:self.curVm.i18n('list.restore','恢复'),
                    defaultBtn:false,
                    "icon": "{type:'jpaasIcon',icon:'iconVue3jpaas-icon_huifu'}",
                },
                {
                    id:"supspendInst",
                    method:'supspendInst',
                    style:false,
                    name:self.curVm.i18n('list.suspend','暂停'),
                    show:self.getStatus('RUNNING'),
                    defaultBtn:false,
                    "icon": "{type:'jpaasIcon',icon:'iconplayer-play-circle'}",
                },
                {
                    show:self.canCancel(),
                    id:"doDiscard",
                    method:'doDiscard',
                    style:false,
                    name:self.curVm.i18n('list.cancel','作废'),
                    defaultBtn:false,
                    "icon": "{type:'jpaasIcon',icon:'iconVue3jpaas-icon_zuofei'}",
                }

            ]

            let btnData = [...defaultBtn.filter(item=>{
                return item.defaultBtn;
            })];

            //按钮进行合并。
            if(this.curVm.buttons.length > 0){
                btnData = this.curVm.buttons.map(item=>{
                    let hasBtn = defaultBtn.find(btn=>{
                        return btn.id == item.id
                    });

                    return hasBtn ? {...hasBtn,...item} : {...item};
                })
            }

            //对条件进行处理。
            btnData = btnData.filter(item=>{
                if(!item.hasOwnProperty('show')){
                    return true;
                }
                return  item.show;
            })

            console.info(JSON.stringify(btnData));



            //找到独立激活的按钮，先删除，再添加到末尾。
            let hasStyleIndex = btnData.findIndex(item=>{
                return item.style=="true" || item.style==true;
            });
            if(hasStyleIndex!=-1){
                let _data = btnData.splice(hasStyleIndex,1);
                btnData.push(_data[0]);
            }

            this.localButtons=this.changeBtnHandle(this.bpmActionType,btnData);
        },
        changeBtnHandle(bpmActionType,btnData){
            let newBtns = [];
            if(!bpmActionType ){dfebu
                return btnData;
            }
            if(!btnData || btnData.length==0){
                return newBtns;
            }
            for(let btn of btnData){
                let isShow = this.canShowBtn(bpmActionType,btn);
                if(isShow){
                    newBtns.push(btn);
                }
            }
            return newBtns;
        },
        /**
         * 处理按钮是否显示
         * @param btn
         */
        canShowBtn(bpmActionType,btn){
            let processConfig = this.curVm.bpmInstDetail
            if(!btn.config || !btn.config.showScript){
                return true;
            }
            let showScript = btn.config.showScript;

            //当前登陆人
            //上下文
            let context ={
                instId:""
            };
            context = { ...context,...this.curVm.user };
            //表单数据
            let formJson=this.getFormData(processConfig.formData);
            //流程实例ID
            context.instId =processConfig.bpmInst.instId;
            //流程状态
            context.instStatus =processConfig.bpmInst.status;
            //流程节点数据
            if(processConfig.userTaskConfig){
                context.bpmNodeId =processConfig.userTaskConfig.key;
                context.bpmNodeCode =processConfig.userTaskConfig.alias;
                context.bpmNodeName =processConfig.userTaskConfig.name;
            }

            try {
                let func=new Function("data","context",btn.config.showScript);
                let isShow =func.bind(this.curVm)(formJson,context);
                return isShow;
            }catch (e){
                console.log("是否显示按钮计算出错。默认不显示，错误信息：  "+e);
            }
            return false;
        },
        getFormData(formData){
            let formJson = {};
            if(formData.data && formData.data.length>0){
                formData.data.forEach(item=>{
                    formJson[item.alias] = item.data;
                })
            }
            return formJson;
        },
    },
    watch:{
        "curVm.bpmInst":{
            handler:function (val, oldVal) {
                this.initButtons();
            },
            deep:true

        }
    }
}
